package de.gameduell.framework.gui.button {
	import de.gameduell.framework.gui.util.ToolTip;
	import de.gameduell.framework.locale.Locale;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author cbra
	 */
	public class ToggleButton extends Button {
		
		public static const OVER_TOGGLED:String 	 = "overToggled";
		public static const UP_TOGGLED:String 	 	 = "upToggled";
		public static const DOWN_TOGGLED:String 	 = "downToggled";
		public static const DISABLED_TOGGLED:String  = "disabledToggled";
		public static const CLICK_TOGGLED:String 	 = "clickToggled";
			
		private const TOGGLED:String = "toggled";
		
		protected var _toggled:Boolean;
		protected var _toggleToolTip:ToolTip;
		
		protected var upToggledState:ButtonState;
		protected var downToggledState:ButtonState;
		protected var overToggledState:ButtonState;
		protected var disabledToggledState:ButtonState;
		
		private var toggleStates:Array;
		
		private const TYPE:String = "toggleButton";
		
		/**
		 * constructor 
		 * @param aClip:Sprite the Sprite the ToggleButton is created from
		 * @param aId:String   optional String to identify the corresponding xml tag in the language xml
		 */
		public function ToggleButton(aClip:Sprite, aId:String = null, aResizeHitterEnabled:Boolean = true, applyStyle:Boolean = false, customStyleId:String = null) {
			super(aClip, aId, aResizeHitterEnabled, applyStyle, customStyleId); 
			_toggled = false;
			_toggleToolTip = null;
			type = TYPE;
			
			upToggledState   	 = createToggleState(UP_TOGGLED);
			downToggledState 	 = createToggleState(DOWN_TOGGLED);
			overToggledState 	 = createToggleState(OVER_TOGGLED);
			disabledToggledState = createToggleState(DISABLED_TOGGLED);
			
			toggleStates = new Array(upToggledState,downToggledState,overToggledState,disabledToggledState);
			if(aId != null){
				locale.localize(this);
			}
					
			addChild(upState.sprite);	
		}
		
		/*###################################################
		#		   			  SETTER						#
		#####################################################*/
			

		/**
		 *  <code>
		 *	var popup:Popup = new Popup("ExamplePopup", "ExamplePopup", 0, true); <br>
		 *	var normalButton:Button = popup.registerButton("normalButton", null, -1, true, "ExamplePopup.normalButton"); <br>
		 *	var toggleButton:ToggleButton =  popup.registerToggleButton("toggleButton", null, -1, true,"ExamplePopup.toggleButton"); <br>
		 *	toggleButton.setToggleTextFromLocale("ExamplePopup.toggleButton.toggledText", true, AutoSize.CENTER); <br>
		 *	popup.show(); <br>	
		 *	</code>
		 * 
		 * @param localeId:String 	 the locale id for the toggle text
		 * @param isHtml:Boolean   sets if the Buttontext is shown as HTML text
		 * @param aAutoSize:String sets if and how the Buttonbackground resizes and how the buttonbackground and the text align themself
		 */
		public function setToggleTextFromLocale(localeId:String, isHtml:Boolean, aAutoSize:String):void {
			var localizedText:String = Locale.getInstance().loadString(localeId);
			setToggleText(localizedText, isHtml, aAutoSize);
		}
		
		/**
		 * @param aText:String 	 the text that the ToggleButton will show in the toggles state
		 * @param isHtml:Boolean   sets if the Buttontext is shown as HTML text
		 * @param aAutoSize:String sets if and how the Buttonbackground resizes and how the buttonbackground and the text align themself
		 */
		public function setToggleText(aText:String, isHtml:Boolean, aAutoSize:String):void {
			for(var i:* in toggleStates){
				var state:ButtonState = toggleStates[i];
				state.setText(aText, isHtml, aAutoSize);
			}
		}
		
		/**
		 * @param useToolTip:Boolean   sets if the Tooltip and the Tooltip for the toggled state for the ToggleButton are activated
		 */
		override public function set toolTip(useToolTip:Boolean):void{
			if(useToolTip && _toolTip == null){
				_toolTip = new ToolTip("tooltip",sprite.name+".alt",sprite,false);
				_toggleToolTip = new ToolTip("tooltip",sprite.name+".alttoggled",sprite,false);
				_toolTip.active = !_toggled && _active;
				_toggleToolTip.active = _toggled && _active;		
			}
		}
		
		public function getToggleTooltip():ToolTip{
			return _toggleToolTip;
		}
		
		
		/**
		 * activates or deactivates the ToggleButton
		 */
		override public function set active(isActive:Boolean):void{
			mouseEnabled = isActive;	
			_active = isActive;
			if(isActive){
				setState(_toggled ? upToggledState : upState);	
			}else{
				setState(_toggled ? disabledToggledState : disabledState);	
			}
		}
		
		/**
		 * @param aOffset:int  Sets the Offset of the Buttontext. The Size of the Button changes accordingly.
		 */
		override public function set offset(aOffset:int):void{
			super.offset = aOffset;
			for(var i:* in toggleStates){
				toggleStates[i].offset = aOffset;
			}
		}
		
		public function get toggled():Boolean {
			return _toggled;
		}
		
		public function getToggleStates():Array{
			return toggleStates;
		}

		/**
		 * @param isToggled:Boolean  sets if the Button is toggled
		 */
		public function set toggled(isToggled:Boolean):void{
			if(_toggled != isToggled){
				_toggled = isToggled;
				setState(_toggled ? upToggledState : upState);
				if(_toolTip != null){
					_toolTip.active = !_toggled && _active;
					_toggleToolTip.active = _toggled && _active;
				}
			}
		}
		
		/*###################################################
		#		   			  INTERNAL						#
		#####################################################*/
	
	
		/**
		 * called on each mouseEvent
		 * 
		 * @param event thrown MouseEvent
		 */
		override protected function mouseEvent(event:MouseEvent):void{
			switch(event.type){
				case MouseEvent.MOUSE_OVER:	
					setState(_toggled ? overToggledState : overState);
					break;
					
				case MouseEvent.MOUSE_DOWN:
					setState( _toggled ? downToggledState : downState);
					break;
					
				case MouseEvent.MOUSE_OUT:
					setState( _toggled ? upToggledState : upState);
					break;
					
				case MouseEvent.MOUSE_UP:
					toggled = !_toggled; // TODO - keep an eye on it!
					if(!_toggled){
						dispatchEvent(new ButtonEvent(CLICK_TOGGLED, this));
					}else{
						dispatchEvent(new ButtonEvent(CLICK,this));
					}
					break;
			}
			
			dispatchEvent(new ButtonEvent(currState.type,this));

			if(sounds[event.type]!= undefined && sounds[event.type]!= null){
				audio.playSound(sounds[event.type]);
			}
		}
		
		protected function createToggleState(type:String):ButtonState{
			var stateSprite:Sprite = getChildByName(type) as Sprite;
			if(stateSprite == null){
				return upToggledState;
			}
			var state:ButtonState = new ButtonState(this,stateSprite,type,id+ Locale.SEPARATOR+TOGGLED);	
			removeChild(state.sprite);
			return state;	
		}
	}
}